package com.fau.wm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalLayerPosition;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 储位管理Action
 * @author liy
 * @datetime 2014年7月27日,下午4:50:47
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class ShelfAction extends PublicAction implements ModelDriven<MedicalLayerPosition> {

	private static final long serialVersionUID = -7246962433801865162L;
	
	private MedicalLayerPosition layerPosition = new MedicalLayerPosition();
	
	/**
	 * @desc 删除储位信息
	 * @return null
	 * @author liy
	 * @datetime 2014年8月5日,下午3:02:52
	 */
	@Action(value = "position_delete")
	public String delete() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(layerPosition.getId())){
			boolean flag = this.baseAllService.getBaseDataService().deleteLayerPosition(this.layerPosition);
			if(flag){
				this.map.put("success", true);
			}else{
				this.map.put("success", false);
			}
			formatJSON(response, this.map);
		}
		return null;
	}
	
	/**
	 * @desc 根据药品名称模糊查询
	 * @return String
	 * @author liy
	 * @datetime 2014年8月1日,下午9:31:24
	 */
	@Action(value = "medical_like_find")
	public String medicalLikeFind() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String medicalName = request.getParameter("key");
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.list = this.baseAllService.getMedicineService().findMedicineByName(admin, medicalName);
		formatJSON(response, this.list);
		return null;
	}
	
	/**
	 * @desc 保存储位信息
	 * @return null
	 * @author liy
	 * @datetime 2014年7月28日,下午3:12:06
	 */
	@Action(value = "position_save")
	public String save() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		MedicalAdmin currUser = (MedicalAdmin) auth.getPrincipal();
		
		this.layerPosition.setOperator(currUser.getName());
		
		boolean flag = this.baseAllService.getBaseDataService().saveLayerPosition(this.layerPosition, currUser);
		
		if(flag) {
			this.map.put("success", true);
		} else {
			this.map.put("success", false);
			this.map.put("message", "该库位已经存在！请输入其他库位");
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 编辑并保存储位信息
	 * @return null
	 * @author liy
	 * @datetime 2014年8月1日,下午9:25:12
	 */
	@Action(value = "shelf_edit_save")
	public String edit() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		MedicalAdmin currUser = (MedicalAdmin) auth.getPrincipal();
		
		this.layerPosition.setOperator(currUser.getName());
		if(StringUtils.isNotBlank(layerPosition.getId())){
			boolean flag = this.baseAllService.getBaseDataService().editLayerPosition(this.layerPosition);
			if (flag) {
				this.map.put("success", true);
			} else {
				this.map.put("success", false);
				this.map.put("message", "该库位已经存在！请输入其他库位");
			}
		}else{
			map.put("message", "error");
			}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 跳转至储位添加页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年7月27日,下午8:55:45
	 */
	@Action(value = "shelf_add", results = {@Result(location = "shelf_add.jsp")})
	public String shelfAdd() {
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转至储位编辑页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年8月1日,下午9:10:50
	 */
	@Action(value = "shelf_edit", results = {@Result(location = "shelf_edit.jsp")})
	public String shelfEdit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询储位信息
	 * @return null
	 * @author liy
	 * @datetime 2014年7月27日,下午8:01:23
	 */
	@Action(value = "position_page")
	public String shelfPage() {
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		layerPosition.setHospitalId(admin.getEnterpriseId());
		this.pageBean = this.baseAllService.getBaseDataService().findPositionByPage(this.layerPosition, this.map);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}

	/**
	 * @desc 跳转至储位管理页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年7月27日,下午4:57:28
	 */
	@Action(value = "shelf_init", results = {@Result(location = "shelf_init.jsp")})
	public String warehouseList() {
		return SUCCESS;
	}
	
	@Override
	public MedicalLayerPosition getModel() {
		return layerPosition;
	}

}
